package com.yq.dao;

import com.yq.bean.Employee;

import java.sql.*;

public class EmployeeDao {
    //登录
    public Employee login_Employee(Connection con, String name, String pw) throws SQLException {
        String sql = "select * from t_employee where login_name = ? and pass_word = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, pw);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            int employee_id = resultSet.getInt("employee_id");
            String employee_no = resultSet.getString("employee_no");
            String employee_name = resultSet.getString("employee_name");
            String login_name = resultSet.getString("login_name");
            String pass_word = resultSet.getString("pass_word");
            String job = resultSet.getString("job");
            Date hiredate = resultSet.getDate("hiredate");
            double sal = resultSet.getDouble("sal");
            int role = resultSet.getInt("role");
            Employee employee = new Employee(employee_id, employee_no, employee_name, login_name, pass_word, job, hiredate, sal, role);
            return employee;
        }
        return null;
    }

    //获取身份
    public Integer get_Role(Connection con, Integer employee_id) throws SQLException {
        String sql = "select role from t_employee where employee_id = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, employee_id);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()) {
            return resultSet.getInt("role");
        }
        return null;
    }

    //获取no
    public String get_NO(Connection con, Integer employee_id) throws SQLException {
        String sql = "select employee_no from t_employee where employee_id = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, employee_id);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()) {
            return resultSet.getString("employee_no");
        }
        return null;
    }

    //用户注册
    public boolean Register_Employee(Connection con, Employee employee) throws SQLException {
        //直接设置为普通员工
        String sql = "insert into t_employee(employee_no,employee_name,login_name,pass_word,job,hiredate,sal,role) " +
                "values (?,?,?,?,?,?,?,0)";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, employee.getEmployee_no());
        ps.setString(2, employee.getEmployee_name());
        ps.setString(3, employee.getLogin_name());
        ps.setString(4, employee.getPass_word());
        ps.setString(5, employee.getJob());
        ps.setDate(6, employee.getHiredate());
        ps.setDouble(7, employee.getSal());
        int i = ps.executeUpdate();
        return i > 0;
    }
}
